package com.example.provider.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api("rabbitmq发送消息")
public class rabbitProviderController {

    @Autowired
    AmqpTemplate amqpTemplate;

    @ApiOperation("发送topic模式下消息")
    @PostMapping("/sendTopicMessage")
    public String sendTopicMessage(String routeKey,String message){
        long beginTime = System.currentTimeMillis();
        amqpTemplate.convertAndSend("topicExchange", routeKey, message);
        long duringTime = System.currentTimeMillis() - beginTime;
        return ""+duringTime;
    }

    @ApiOperation("发送direct模式下消息")
    @PostMapping("/sendDirectMessage")
    public String sendDirectMessage(String message){
        long beginTime = System.currentTimeMillis();
        amqpTemplate.convertAndSend("queue", message);
        long duringTime = System.currentTimeMillis() - beginTime;
        return ""+duringTime;
    }

    @ApiOperation("发送fanout模式下消息")
    @PostMapping("/sendFanoutMessage")
    public String sendFanoutMessage(String message){
        long beginTime = System.currentTimeMillis();
        amqpTemplate.convertAndSend("fanoutExchange","", message);
        long duringTime = System.currentTimeMillis() - beginTime;
        return ""+duringTime;
    }
}
